package com.example.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.pojo.LoginUser;
import com.example.pojo.SbMessageVo;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Mapper
public interface SbMessageMapper extends BaseMapper<SbMessageVo> {

    @Select("select a.*  from t_gps_equipment_information a left join t_user_gps_association b  on a.imei =b.imei \n" +
            "left join t_gps_login_user c on b.userid =c.id where c.login_code =#{username}")
    public List<SbMessageVo> UserSbMessage(@Param("username") String username) ;

//    @Select("select  CONCAT(substr(latitude,1,2),'.',substr(latitude,2,LENGTH(latitude))) as latitude,  CONCAT(substr(longitude,1,3),'.',substr(longitude,3,LENGTH(longitude))) as   longitude ,period_of_validity ,b.gpsname from  gps_data a left join t_gps_equipment_information b  on a.imei =b.imei   where a.imei =#{sbid} and data_time=(select max(data_time)from gps_data  where imei =#{sbid} group by  imei) LIMIT 1 ")
    @Select("select  latitude,  longitude ,period_of_validity ,b.gpsname,a.data_time, a.speed from  gps_data a left join t_gps_equipment_information b  on a.imei =b.imei   where a.imei =#{sbid} and data_time=(select max(data_time)from gps_data  where imei =#{sbid} group by  imei) LIMIT 1 ")
    SbMessageVo devicLocation(@Param("sbid") String sbid);
//    @Select("select CONCAT(substr(latitude,1,2),'.',substr(latitude,2,LENGTH(latitude))) as latitude,  CONCAT(substr(longitude,1,3),'.',substr(longitude,3,LENGTH(longitude))) as   longitude    from  gps_data   where imei =#{sbid} and DATE(data_time) BETWEEN DATE(#{startTime}) and DATE(#{endTime}) ORDER BY DATE(DATA_TIME) DESC")
    @Select("select latitude,  longitude     from  gps_data   where imei =#{sbid} and DATE(data_time) BETWEEN DATE(#{startTime}) and DATE(#{endTime}) ORDER BY DATE(DATA_TIME) DESC")
    List<Map<String, Double>> deviceTrackPlayBack(@Param("sbid") String sbid, @Param("startTime")String  startTime, @Param("endTime")String endTime);

    @Select("select id from t_gps_login_user where login_code =#{username}")
    LoginUser selectUserInfo(String username);

    @Select("select * from t_gps_electronic_fence where imei =#{sbid}")
    List<Map<String, Object>> seletElectronicFenceInformation(@Param("sbid") String sbid);
//    @Select("select CONCAT(substr(latitude,1,2),'.',substr(latitude,2,LENGTH(latitude))) as latitude,  CONCAT(substr(longitude,1,3),'.',substr(longitude,3,LENGTH(longitude))) as   longitude from gps_data where imei =#{sbid}")
    @Select("select latitude,  longitude  from gps_data where imei =#{sbid} and (latitude != 0.0 or longitude !=0.0)   ")
    List<Map<String,Double>> selectGpsDate(@Param("sbid") String sbid);

    @Select("select imei,center_point_coordinates_longitude,center_point_coordinates_latitude,radius from t_gps_electronic_fence where sfqy ='1'")
    List<Map<String,String>> seletALlElectronicFenceInformation();

    @Update("update t_gps_electronic_fence set sfqy=#{flag} where imei=#{sbid}")
    void updateElect(@Param("sbid") String sbid,@Param("flag") String flag);


    @Select("select * from t_gps_electronic_fence where sfqy ='1' and imei =#{sbid}")
    Map<String, Object> seletOpenElectronicFenceInformation(@Param("sbid") String sbid);

    @Select("select phone_num from t_gps_login_user a left join  t_user_gps_association b on a.id =b.userid\n" +
            "where imei =#{sbid}\n")
    LoginUser getDeviceUser(@Param("sbid") String sbid);

    @Insert("insert into t_gps_electronic_fence (imei,center_point_coordinates_longitude,center_point_coordinates_latitude,radius) values (#{imei},#{center_point_coordinates_longitude},#{center_point_coordinates_latitude},#{radius})")
    void SaveElectronicFenceInformation(Map<String, String> map);

    @Delete("delete from t_gps_electronic_fence  where imei=#{imei}")
    void deleteElectIcInformation(@Param("imei") String imei);

    @Select("select sfqy from  t_gps_electronic_fence where imei =#{sbid}")
    String openState(@Param("sbid") String sbid);
}
